package com.kadron.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;

import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.intercept.BypassInterceptors;

import com.kadron.entity.Order;

@AutoCreate
@BypassInterceptors
@Name(KOrderDAO.K_NAME)
public class KOrderDAO extends KDAO<Order> {
	
	public static final String K_NAME = "orderDAO";

	@SuppressWarnings("unchecked")
	public List<Order> getOrders(long userId) {
		EntityManager em = getEntityManager();
		return em.createQuery("SELECT e FROM Order e WHERE e.user.id=:uid ORDER BY e.id ASC")
		.setParameter("userId", userId)
		.getResultList();
	}
	
	public Order find(long orderId) {
		EntityManager em = getEntityManager();
		try {			
			return (Order) em.createQuery("SELECT e FROM Order e WHERE e.id=:id")
			.setParameter("id", orderId)
			.setMaxResults(1)
			.getSingleResult();
		} catch (NoResultException e) {
			return null;
		}
	}
	
}
